package com.intel.factorytest.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.intel.factorytest.R;
import com.intel.factorytest.activity.BaseActivity;
import com.intel.factorytest.activity.SensorsTestActivity;

public class GridAdapter extends BaseAdapter {
    private static final String TAG = "GridAdapter";

    private Context context;
    private BaseActivity mBaseActivity;
    private GridView mGridView;
    private Button mSucBtn;
    private int horizontalSpacing = 0;
    private int verticalSpacing = 0;
    private List<SensorsTestActivity.ItemInfo> infoList;

    public GridAdapter(Context context,
            List<SensorsTestActivity.ItemInfo> infoList) {
        this.context = context;
        this.mBaseActivity = (BaseActivity) context;
        this.infoList = infoList;

        // 获取单元格之间的间距
        mGridView = (GridView) (((RelativeLayout) LayoutInflater.from(context)
                .inflate(R.layout.sensor_gridview, null))
                .findViewById(R.id.gridview));
        horizontalSpacing = mGridView.getRequestedHorizontalSpacing();// .getHorizontalSpacing();
        verticalSpacing = mGridView.getVerticalSpacing();
    }

    public int getCount() {
        return infoList.size();
    }

    public Object getItem(int item) {
        return infoList.get(item);
    }

    public long getItemId(int id) {
        return id;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder viewHolder = null;
        int hight;
        int width;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.gridview_item, null);
            // DisplayMetrics metric = new DisplayMetrics();
            // mBaseActivity.getWindowManager().getDefaultDisplay().getMetrics(metric);
            // if
            // (mBaseActivity.getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE)
            // {
            // hight =
            // (metric.widthPixels-mButtonLayout.getMeasuredHeight()-verticalSpacing)/2;
            // width = (metric.heightPixels-horizontalSpacing)/3;
            // }
            // else
            // {
            // hight = (metric.heightPixels-verticalSpacing)/3;
            // width
            // =(metric.widthPixels-mButtonLayout.getMeasuredHeight()-horizontalSpacing)/2;
            // }
            //
            // hight = (metric.heightPixels-verticalSpacing)/3;
            // width
            // =(metric.widthPixels-mButtonLayout.getMeasuredHeight()-horizontalSpacing)/2;
            // Log.d(TAG,
            // "horizontalSpacing:"+horizontalSpacing+";  verticalSpacing:"+verticalSpacing);
//            Log.d(TAG, "mSucBtn.getMeasuredWidth() :" + mSucBtn.getMeasuredWidth()
//                    + ";  mSucBtn.getMeasuredHeight():" + mSucBtn.getMeasuredHeight());
//            Log.d(TAG, "mBaseActivity.lenX :" + mBaseActivity.lenX
//                    + ";  mmBaseActivity.lenY:" + mBaseActivity.lenY);
            // AbsListView.LayoutParams params = new
            // AbsListView.LayoutParams(mBaseActivity.lenX - horizontalSpacing /
            // 2 , mBaseActivity.lenY - verticalSpacing / 2);
            // convertView.setLayoutParams(params);
//            convertView.setLayoutParams(new GridView.LayoutParams(
//                    mBaseActivity.lenX - horizontalSpacing / 2,
//                    mBaseActivity.lenY - verticalSpacing / 2));
            convertView.setLayoutParams(new GridView.LayoutParams(
                    LayoutParams.MATCH_PARENT, mBaseActivity.lenY - verticalSpacing/2));
            convertView.setBackgroundColor(Color.WHITE);
            TextView nametv = (TextView) convertView
                    .findViewById(R.id.sensor_name);
            TextView valuetv = (TextView) convertView
                    .findViewById(R.id.sensor_value);
            // LinearLayout itemLayout = (LinearLayout)
            // convertView.findViewById(R.id.gridviewitem);
            LinearLayout titleLayout = (LinearLayout) convertView
                    .findViewById(R.id.sensortitlelayout);
            LinearLayout resultLayout = (LinearLayout) convertView
                    .findViewById(R.id.sensorresultlayout);

            viewHolder = new ViewHolder(nametv, valuetv, resultLayout,
                    titleLayout);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        SensorsTestActivity.ItemInfo ii = infoList.get(position);
        viewHolder.nametv.setText(ii.name);
        viewHolder.valuetv.setText(ii.value);
        if (!ii.toTest) {
            viewHolder.resultLayout.setBackgroundColor(Color.GRAY);
            viewHolder.titleLayout.setBackgroundColor(Color.GRAY);
            viewHolder.valuetv.setText(context
                    .getString(R.string.ComplexTestItem_Undefined));
        }
        if (ii.isAvaliable) {
            viewHolder.resultLayout.setBackgroundColor(Color.GREEN);
            viewHolder.titleLayout.setBackgroundColor(Color.GREEN);
        } else {
            if (ii.toTest) {
                viewHolder.resultLayout.setBackgroundColor(Color.WHITE);
                viewHolder.titleLayout.setBackgroundColor(Color.WHITE);
            }
        }
        if (ii.isRedBg) {
            viewHolder.resultLayout.setBackgroundColor(Color.RED);
            viewHolder.titleLayout.setBackgroundColor(Color.RED);
        }

        return convertView;
    }

    private class ViewHolder {
        TextView nametv;
        TextView valuetv;
        LinearLayout resultLayout;
        LinearLayout titleLayout;

        ViewHolder(TextView nametv, TextView valuetv,
                LinearLayout resultLayout, LinearLayout titleLayout) {
            this.nametv = nametv;
            this.valuetv = valuetv;
            this.resultLayout = resultLayout;
            this.titleLayout = titleLayout;
        }
    }

}
